﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;

public partial class Admin_FinSav_FinSav002 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).Date.ToString(ConstantValue.DateFormat);
            txtEndate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);


            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["drpStatus"] != null)
            {
                drpStatus.SelectedValue = Session["drpStatus"].ToString();
            }
            if (IsAdmin)
            {
                panThirdUnit.Visible = true;

                if (Session["ThirdUnit"] != null)
                {
                    drpThirdUnit.SelectedValue = Session["ThirdUnit"].ToString();
                }
            }

            if (Session["drpDept"] != null)
            {
                drpDept.SelectedValue = Session["drpDept"].ToString();
            }
            if (Session["txtDocNo"] != null)
            {
                txtDocNo.Text = Session["txtDocNo"].ToString();
            }
        }
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["drpStatus"] = drpStatus.SelectedValue;
        if (IsAdmin)
        {
            Session["ThirdUnit"] = drpThirdUnit.SelectedValue;
        }
        Session["drpDept"] = drpDept.SelectedValue;
        Session["txtDocNo"] = txtDocNo.Text;
    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinReceiptQuery q1 = new FinReceiptQuery("q1");

        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q6 = new TSCodeTableQuery("q6");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");
        BFCompanyDicQuery q7 = new BFCompanyDicQuery("q7");
        VReceiptExtInfoQuery q8 = new VReceiptExtInfoQuery("q8");
        TSCodeTableQuery q9 = new TSCodeTableQuery("q9");

        q1.LeftJoin(q9).On(q1.VoucherDescription == q9.Code);

        q1.LeftJoin(q2).On(q1.FunctionDept == q2.Code);
        q1.LeftJoin(q3).On(q1.StorageStatus == q3.Code);
        q1.LeftJoin(q4).On(q1.DocumentsType == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageLocation == q5.ThirdUnitID);
        q1.LeftJoin(q6).On(q1.Division == q6.Code);
        q1.LeftJoin(q7).On(q1.CompanyCode == q7.CompanyCode);
        q1.LeftJoin(q8).On(q1.ReceiptID == q8.ReceiptID);

        q1.SelectAll().Select(q2.Value1.As("FunctionDeptStr"), q3.Value1.As("StorageStatusStr"), q4.Value1.As("DocumentsTypeStr"), q5.Name.As("StorageLocationStr"), q6.Value1.As("DivisionStr"), q7.CompanyName, q7.CompanyCode, q8.SupplierBoxNo, q8.BoxNo, q9.Value1.As("DocDesc"));

        if (txtDocNo.Text.Length > 0)
        {
            q1.Where(q1.DocumentsNo.Like("%" + txtDocNo.Text.Trim() + "%") || q1.Reference.Like("%" + txtDocNo.Text.Trim() + "%") || q1.ReferenceKey.Like("%" + txtDocNo.Text.Trim() + "%"));
        }
        if (drpDept.SelectedIndex > 0)
        {
            q1.Where(q1.FunctionDept == drpDept.SelectedValue);
        }
        if (drpStatus.SelectedIndex > 0)
        {
            q1.Where(q1.StorageStatus == drpStatus.SelectedValue);
        }
        if (IsAdmin)
        {
            if (drpThirdUnit.SelectedIndex > 0)
            {
                q1.Where(q1.StorageLocation == drpThirdUnit.SelectedValue);
            }
        }
        else
        {
            q1.Where(q1.StorageLocation == LoginUserInfo.CurrentUser.ThirdUnitID);
        }

        q1.Where(q1.IsSAP == 1 && q1.CreatedDttm >= txtStartDate.Value && q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        q1.OrderBy(q1.CreatedDttm.Descending);

        DataTable tableSorce = q1.LoadDataTable();
        pageGrid.DataSource = tableSorce;
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("drpStatus");
        if (IsAdmin)
        {
            Session.Remove("ThirdUnit");
        }
        Session.Remove("drpDept");
        Session.Remove("txtDocNo");
    }

    protected string GetActionUrl(string status, object rowView, string boxNo, bool isSAP)
    {
        string retUrl = "";

        if (status != ConstantValue.StorageStatusCode.Loan && status != ConstantValue.StorageStatusCode.PendingTransfer && status != ConstantValue.StorageStatusCode.Transfer && string.IsNullOrEmpty(boxNo))
        {
            retUrl = GetUpdateLink("FinSav001a.aspx", rowView, 700, 680, FinReceiptMetadata.ColumnNames.ReceiptID);

            if (!isSAP)
            {
                retUrl += "   " + GetLink("删除", "D", "FinSav001a.aspx", rowView, 700, 680, false, FinReceiptMetadata.ColumnNames.ReceiptID);
            }

        }

        return retUrl;
    }
}